package com.hue.gulimail.gulimailorder.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hue.gulimail.gulimailorder.pay.PayAsyncVo;
import com.hue.gulimail.gulimailorder.service.PaymentInfoService;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hue.gulimail.gulimailorder.entity.OrderEntity;
import com.hue.gulimail.gulimailorder.service.OrderService;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 订单
 *
 * @author yaofengqiao
 * @email 451737523@qq.com
 * @date 2021-08-12 16:21:08
 */
@RestController
@RequestMapping("gulimailorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PaymentInfoService paymentInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimailorder:order:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimailorder:order:info")
    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimailorder:order:save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimailorder:order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimailorder:order:delete")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getBySn/{orderSn}")
    public R getBySn(@PathVariable("orderSn") String orderSn) {
        OrderEntity order = orderService.getBySn(orderSn);
        return R.ok().put("data", order);
    }

    /**
     * 获取一个用户的订单信息
     *
     * @return
     */
    @PostMapping("/memberOrder")
    public R memberOrder() {
        List<OrderEntity> orders = orderService.getMemberOrders();

        return R.ok().put("data", orders);
    }

    /**
     * 支付宝支付成功之后异步调用的接口,只有返回了支付宝success这个字符串之后支付宝才不会继续通知
     *
     * @return
     */
    @PostMapping("/paySuccess")
    public String paySuccess(PayAsyncVo asyncVo) {
        //获取订单号
        String orderSn = asyncVo.getOut_trade_no();

        //将订单修改为已支付状态
        orderService.updateStatusToPay(orderSn);

        //保存流水信息
        paymentInfoService.add(asyncVo);

        return "success";
    }
}
